package de.unibremen.mum.app.whatscool;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created with IntelliJ IDEA. User: Marten Date: 11.06.13 Time: 13:47 To change
 * this template use File | Settings | File Templates.
 */
public class GalleryFragment extends Fragment {
	private ImageAdapter mAdapter;
	private DatabaseHandler db;
	private ArrayList<RatingClass> ratinglist;

	// Empty constructor as per Fragment docs
	public GalleryFragment() {
	}

	/*
	 * The system calls this when creating the fragment. Within your
	 * implementation, you should initialize essential components of the
	 * fragment that you want to retain when the fragment is paused or stopped,
	 * then resumed.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mAdapter = new ImageAdapter(getActivity());
		db = new DatabaseHandler(getActivity());
		ratinglist = db.getAllRatings();
	}

	/*
	 * The system calls this when it's time for the fragment to draw its user
	 * interface for the first time. To draw a UI for your fragment, you must
	 * return a View from this method that is the root of your fragment's
	 * layout. To return a layout from onCreateView(), you can inflate it from a
	 * layout resource defined in XML. To help you do so, onCreateView()
	 * provides a LayoutInflater object.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View galleryView = inflater.inflate(R.layout.activity_gallery, container, false);
		GridView mGridView = (GridView) galleryView.findViewById(R.id.gridview);
		mGridView.setAdapter(mAdapter);

		mGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				// Starte ResultActivity
				long ratingId = ratinglist.get(position).getId();
				Intent resultLast = new Intent(getActivity(), ResultActivity.class);
				resultLast.putExtra("id",ratingId);
	            startActivity(resultLast);
			}
		});

		return galleryView;
	}

	private class ImageAdapter extends BaseAdapter {

		public ImageAdapter(Context context) {
			super();
		}

		@Override
		public int getCount() {
			return ratinglist.size();
		}

		@Override
		public Object getItem(int position) {
			return ratinglist.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup container) {
			View view;
			if (convertView == null) { // if it's not recycled, initialize some
										// attributes
				LayoutInflater li = getActivity().getLayoutInflater();
				view = li.inflate(R.layout.gallery_image, null);
				RatingClass rating = ratinglist.get(position);
				//Bild zum View hinzufügen
				String imageUrl = rating.getPic();
				ImageView iv = (ImageView) view.findViewById(R.id.gallery_image_icon);
				if ((new File(imageUrl)).exists()) {
					Bitmap bitmap = BitmapFactory.decodeFile(imageUrl);
					iv.setImageBitmap(bitmap); // Load image into ImageView
				}
				//Name zum View hinzufügen
				TextView tv = (TextView) view.findViewById(R.id.gallery_image_text);
				tv.setText(rating.getName());

				view.setLayoutParams(new GridView.LayoutParams(150, 220));

			} else {
				view = convertView;
			}

			return view;
		}

	}
}
